/*
 * Copyright (C) 2013  Forklabs Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.soap.axis.client.util;

import java.util.HashMap;
import java.util.Map;
import ca.gc.dfo.wds.Metadata;

/**
 * Class {@code MetadataUtil} offers methods to work with {@link Metadata}.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.soap.axis.client.util.MetadataUtil">Daniel Léonard</a>
 * @version $Revision$
 */
public abstract class MetadataUtil {

//---------------------------
// Constructors
//---------------------------

   /**
    * Constructor
    */
   protected MetadataUtil() {
   // rien
      }

//---------------------------
// Class methods
//---------------------------

   /**
    * Converts the array of {@link Metadata} into a map keyed by the metadata's
    * name and indexing the metadata's value.
    * @param   metadatas   the array of {@code Metadata}.
    * @return   the map.
    */
   public static Map<String, String> asMap(Metadata[] metadatas) {
      Map<String, String> map = new HashMap<>();
      for (Metadata metadata : metadatas) {
         String name = metadata.getName();
         String value = metadata.getValue();
         map.put(name, value);
         }
      return map;
      }

   /**
    * Gets the value of the metadata with the given name.
    * <p>
    * This method calls {@link #asMap(Metadata[])}, gets the value and discards
    * the map. If more than one metadata's value are required, it might be
    * better to get them directly from the map.
    *
    * @param   name   the name of the metadata.
    * @param   metadatas   the array of {@code Metadata}.
    * @return   the value if it exists, {@code null} otherwise.
    */
   public static String valueOf(String name, Metadata[] metadatas) {
      Map<String, String> map = asMap(metadatas);
      String value = map.get(name);
      return value;
      }

   }
